package com.example.work_one

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager

class ShopActivity : AppCompatActivity(),FinishActivity {
    private val goodsList=ArrayList<Goods>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_shop)
        create(this)
        initGoods()
        val layoutManager= StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL)
        layoutManager.orientation= LinearLayoutManager.HORIZONTAL
        val adapter=GoodsAdapter(goodsList)
        val recyclerView:RecyclerView=findViewById(R.id.recyclerView)
        recyclerView.layoutManager=layoutManager
        recyclerView.adapter=adapter

    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.main,menu)
        return  true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when(item.itemId){
            R.id.back_item-> Toast.makeText(this,"You clicked Add", Toast.LENGTH_SHORT).show()
            R.id.exit_item-> {
                Toast.makeText(this,"Bye", Toast.LENGTH_SHORT).show()
                ActivityCollector.finishAll()
            }
        }
        return true
    }
    private fun initGoods() {
        repeat(3) {
            goodsList.add(Goods("apple", R.drawable.apple_pic))
            goodsList.add(Goods("bowl", R.drawable.bowl_pic))
            goodsList.add(Goods("camera", R.drawable.camera_pic))
            goodsList.add(Goods("glasses", R.drawable.glasses_pic))
            goodsList.add(Goods("watches", R.drawable.watches_pic))
        }
    }

    override fun onDestroy() {
        destroy(this)
        super.onDestroy()
    }
}